package cn.king.pet.service.impl;

import cn.king.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.king.basic.util.AjaxResult;
import cn.king.pet.domain.Pet;
import cn.king.pet.domain.PetDetail;
import cn.king.pet.domain.PetOnlineAuditLog;
import cn.king.pet.mapper.PetDetailMapper;
import cn.king.pet.mapper.PetMapper;
import cn.king.pet.mapper.PetOnlineAuditLogMapper;
import cn.king.pet.service.IPetService;
import cn.king.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        //修改宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }
    @Transactional
    @Override
    public void delete(Long id) {
        //删除宠物详情
        petDetailMapper.delByPetId(id);
        //删除宠物信息
        super.delete(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.getOne(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if (boo1 && boo2) {
                //改状态
                petMapper.onsale(id, new Date());
                //添加审核日志
                log.setState(1);//审核成功
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);
            } else {
                //添加审核日志
                log.setState(0);//审核失败
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setMsg("审核失败").setSuccess(false);
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public AjaxResult offsale(List<Long> ids) {
        //改状态 //方式1：先查再改   //方式2：直接改需要写sql
        //Mybatis多参数操作：1.Map 2.对象 3.索引下标#{0} 4.注解
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
